package com.tourism.www;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.t2cn.common.www.ServletCommon;
import com.t2cn.util.StringUtil;
import com.tourism.storage.dao.CourseDao;
import com.tourism.storage.dao.PartnerDao;
import com.tourism.storage.dao.PassengersDao;
import com.tourism.storage.dto.PassengersDto;
import com.tourism.storage.pojo.Courses;
import com.tourism.storage.pojo.Partner;
import com.tourism.storage.pojo.Passengers;
import com.tourism.www.common.TourismBaseHandler;

public class PassengersHandler extends TourismBaseHandler {

	@Override
	public String doActionX(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = getMethodName(request);
		if("page_add".equals(method)){
			return toAddPage(request, response);
		}else if("add".equals(method)){
			return addPassengers(request, response);
		}else if("list".equals(method)){
			return list(request, response);
		}else if("delete".equals(method)){
			return delete(request, response);
		}else if("uTeam".equals(method)){
			return updateTeam(request, response);
		}else if("getPaid".equals(method)){
			return getPaid(request, response);
		}else if("teamPassengers".equals(method)){
			return listTeamPassengers(request, response);
		}else if("uStatus".equals(method)){
			return updateStatus(request, response);
		}else if("listByPaid".equals(method)){
			return listByPaid(request, response);
		}
		
		return null;
	}
	private String toAddPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		CourseDao courseDao = new CourseDao();
		List<Courses> coursesList = courseDao.getCourses(0);
		request.setAttribute("coursesList", coursesList);
		
		PartnerDao partnerDao = new PartnerDao();
		List<Partner> partnerList = partnerDao.getPartbers(0);
		request.setAttribute("partnerList", partnerList);
System.out.println("-=-=-=-=-=-=-=-=-=toAddPage-=-=-=-=-=-=-=-=-=");
		return "page_add";
	}
	
	private String addPassengers(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		String contact = ServletCommon.getParamValue(request, "contact");
		Integer adults = ServletCommon.getIntParamValue(request, "adults", 0);
		Integer childs = ServletCommon.getIntParamValue(request, "childs", 0);
		Integer courseid = ServletCommon.getIntParamValue(request, "courseid", 0);
		String departuredate = ServletCommon.getParamValue(request, "departuredate");
		String phone = ServletCommon.getParamValue(request, "phone");
		String fax = ServletCommon.getParamValue(request, "fax");
		Integer price = ServletCommon.getIntParamValue(request, "price", 0);
		Integer paymode = ServletCommon.getIntParamValue(request, "paymode", 0);	
		Integer paid = ServletCommon.getIntParamValue(request, "paid", 0);
		Integer ifrom = ServletCommon.getIntParamValue(request, "ifrom", 0);
		
		Passengers	pojo = new Passengers();
		
		pojo.setAdults(adults);
		pojo.setChilds(childs);
		pojo.setContact(contact);
		pojo.setCourseid(courseid);
		pojo.setDeparturedate(new Timestamp(StringUtil.toYyyyMmDdDate(departuredate, new Date()).getTime()));
		pojo.setFax(fax);
		pojo.setIfrom(ifrom);
		pojo.setPaid(paid);
		pojo.setPaymode(paymode);
		pojo.setPhone(phone);
		pojo.setPrice(price);
		pojo.setTeamid(0);
		pojo.setStatus(0);
		
		PassengersDao dao = new PassengersDao();
		dao.addPassengers(pojo);
		
		return "list";
	}	
	
	private String list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		Integer courseid = ServletCommon.getIntParamValue(request, "courseidw", -1);
		Integer status = ServletCommon.getIntParamValue(request, "statusw", -1);
		Integer paymode = ServletCommon.getIntParamValue(request, "paymodew", -1);
		Integer paid = ServletCommon.getIntParamValue(request, "paidw", -1);
		
		
		Passengers pojo = new Passengers();
		PassengersDao pasDao = new PassengersDao();
		System.out.println("-=-=-=-=-=-=-=-=-=list-=-=-=-=-=-=-=-=-=");
		
		if( courseid >= 0 )
			pojo.setCourseid(courseid);
		if( status >= 0 )
			pojo.setStatus(status);
		if( paymode >= 0 )
			pojo.setPaymode(paymode);
		if( paid >= 0 )
			pojo.setPaid(paid);
		
		//List<Passengers> list = pasDao.getAllPassengerses(pojo);
		List<Passengers> list = pasDao.getPassengerses(pojo);

		List<PassengersDto> dto_list = new ArrayList<PassengersDto>();
		
		for(int i=0; i < list.size(); i++){
			PassengersDto dto = new PassengersDto();
			dto = Pojo2Dto(list.get(i));
			dto_list.add(dto);
		}
		
		pojo.setCourseid(courseid);
		pojo.setStatus(status);
		pojo.setPaymode(paymode);
		pojo.setPaid(paid);
		
		request.setAttribute("pasDto", dto_list);
		System.out.println(pojo);
		System.out.println("Courseid"+pojo.getCourseid());
		request.setAttribute("passengers", pojo);
		return "page_list";
	}
	
	private String listByPaid(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer paid = ServletCommon.getIntParamValue(request, "paid", 0);
		
		Passengers pojo = new Passengers();
		PassengersDao pasDao = new PassengersDao();
		
		pojo.setPaid(paid);

		List<Passengers> list = pasDao.getPassengerses(pojo);
		List<PassengersDto> dto_list = new ArrayList<PassengersDto>();
		
		for(int i=0; i < list.size(); i++){
			PassengersDto dto = new PassengersDto();
			dto = Pojo2Dto(list.get(i));
			dto_list.add(dto);
		}

		pojo.setPaid(paid);
		
		request.setAttribute("pasDto", dto_list);
		return "page_list_finance";
	}
	
	private String listTeamPassengers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);
		
		Passengers pojo = new Passengers();
		pojo.setTeamid(teamid);
		System.out.println("-=-=-=-=-=-=-=-=-=listTeamPassengers-=-=-=-=-=-=-=-=-=");	
		System.out.println("-=-=-=-=-=-=-=-=-=teamid=" + teamid);
		PassengersDao pasDao = new PassengersDao();
		List<Passengers> list = pasDao.getTeamPassengerses(pojo);
		
		List<PassengersDto> dto_list = new ArrayList<PassengersDto>();
//		List<Teams> team_list = new ArrayList<Teams>
		
		for(int i=0; i < list.size(); i++){
			PassengersDto dto = new PassengersDto();
			dto = Pojo2Dto(list.get(i));
			dto_list.add(dto);
		}
		request.setAttribute("pasDto", dto_list);

		return "page_list";
	}
	
	private PassengersDto Pojo2Dto(Passengers pojo){
		PassengersDto dto = new PassengersDto();
		dto.setPassengers(pojo);
		CourseDao courseDao = new CourseDao();
		dto.setCoursename(courseDao.getCourseById(pojo.getCourseid()).getCoursename());
		
		PartnerDao partnerDao = new PartnerDao();
		dto.setPartnername(partnerDao.getPartberById(pojo.getIfrom()).getPartnername());
		return dto;
		
	}
	
	private String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);

		//String address = ServletCommon.getParamValue(request, "address");
		PassengersDao dao = new PassengersDao();
		dao.deletePassengers(infoid);
		
		return "list";
	}
	
	private String updateTeam(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);
		Integer teamid = ServletCommon.getIntParamValue(request, "teamid", 0);

		System.out.println("-=-=-=-=-=-=-=-=-=infoid:" + infoid);
		System.out.println("-=-=-=-=-=-=-=-=-=teamid:" + teamid);
		Passengers pojo = new Passengers();
		pojo.setInfoid(infoid);
		pojo.setTeamid(teamid);
		PassengersDao dao = new PassengersDao();
		dao.updatePassengers(pojo);
		
		return "list";
	}
	
	private String updateStatus(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);
		Integer status = ServletCommon.getIntParamValue(request, "status", 0);

		System.out.println("-=-=-=-=-=-=-=-=-=infoid:" + infoid);
		System.out.println("-=-=-=-=-=-=-=-=-=status:" + status);
		if(status == 0)
			status = 1;
		else
			status = 0;
		Passengers pojo = new Passengers();
		pojo.setInfoid(infoid);
		pojo.setStatus(status);
		PassengersDao dao = new PassengersDao();
		dao.updatePassengers(pojo);
		
		return "list";
	}
	
	private String getPaid(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);
		Integer paid = ServletCommon.getIntParamValue(request, "paid", 0);

		System.out.println("-=-=-=-=-=-=-=-=-=infoid:" + infoid);
		System.out.println("-=-=-=-=-=-=-=-=-=paid:" + paid);

		Passengers pojo = new Passengers();
		if(paid == 0)
			paid = 1;
		else
			paid = 0;
		pojo.setInfoid(infoid);		
		pojo.setPaid(paid);
		PassengersDao dao = new PassengersDao();
		dao.updatePassengers(pojo);

		return "listf";
	}
	
//	private String getPassengersById(HttpServletRequest request,
//			HttpServletResponse response) throws ServletException, IOException{
//		Integer infoid = ServletCommon.getIntParamValue(request, "infoid", 0);
//
//		PassengersDao dao = new PassengersDao();
//		Passengers pojo = dao.getPassengersById(infoid);
//		request.setAttribute("pojo", pojo);
//		return "page_update";
//	}	
}
